﻿
/// <reference path="_namespace.js" />
/// <reference path="Colour.js" />
/// <reference path="GraphicsBase.js" />
/// <reference path="../geometry/shapes/Rectangle.js" />


/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.drawing.Graphics

Note: This class is merely a "wrapper" around the Java applet Canvas in order to
provide a consistant (as possible) interface between this and the std Graphics object.
/*****************************************************************************/

ajax.lang.addNamespace("ajax.drawing");
$imports("ajax.geometry.shapes.Rectangle");

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava = function(doc, id, width, height)
{
    ajax.drawing.GraphicsJava.superConstructor.call(this, doc, width, height);
    
    // Create and Configure Applet
    var sWidth      = typeof(width) != "undefined" ? width : "100%";
    var sHeight     = typeof(height) != "undefined" ? height : "100%";
    var cssWidth    = typeof(width) != "undefined" ? width : "100px";
    var cssHeight   = typeof(height) != "undefined" ? height : "100px";
                
    
    
    if (ajax.Browser.isSafari)
    {          
        // Safari likes this (horrid hack!)
        var div = this.doc.createElement("div");        
        div.innerHTML = "<applet style='width:" + cssWidth + "; height: " + cssHeight + "' mayscript width='" + sWidth + "' height='" + sHeight + "' codebase='" + ajax.getDomainPath() + "/drawing' code='CanvasApplet.class' id='newApplet'></applet>";
        id.appendChild(div); // adds applet to the tree.
        
        // Get a handle back to the applet and null the id
        this._applet = this.doc.getElementById("newApplet");
        this._applet.id = "";
    }
    /*
    else if (navigator.userAgent.indexOf("Gecko") != -1)
    {
        userType = "Firefox";
        // FF & NN
        // FF doesn't like fuck all!
        
    }
    */
    else
    {
        userType = "Internet Explorer";
        // ie likes this (most correct)        
        
        // ie likes this:
        this._applet                = this.doc.createElement("applet"); 
        this._applet.setAttribute("mayscript", true);
        this._applet.codeBase       = ajax.getDomainPath() + "/drawing";
        this._applet.code           = "CanvasApplet.class";
        this._applet.style.width    = typeof(width) != "undefined" ? width : "100%";
        this._applet.style.height   = typeof(height) != "undefined" ? height : "100%";
        this._applet.width          = typeof(width) != "undefined" ? width : "100px";
        this._applet.height         = typeof(height) != "undefined" ? height : "100px";
        var param   = document.createElement("param");
        param.name  = "scriptable";
        param.value = "true";
        var param2  = document.createElement("param");
        param2.name = "mayscript";
        param2.value= "true";   
        
        this._applet.appendChild(param);
        this._applet.appendChild(param2);
        
        id.appendChild(this._applet);
        
    }
    
    // If the applet gets focus, set it to the body, ONLY works on IE    
    ajax.lang.getEvent(this.window, this._applet, "onfocus").addListener(function(e)
        {
            setTimeout(function(){this.doc.body.focus()}.bind(this), 500);
        }.bind(this)        
    );     
              
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.drawing.GraphicsJava", "ajax.drawing.GraphicsBase"); // inherit
//-----------------------------------------------------------------------------

// Scale is always 1.
ajax.drawing.GraphicsJava.prototype.getScale = function()
{     
    return 1; 
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.setSize = function(w, h)
{
    this._applet.style.width = w + "px";
    this._applet.style.height = h + "px";
    this._applet.createGraphics(w, h);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.dispose = function()
{   
    try
    {             
        this._applet.stop();
        this._applet.destroy();
    }
    catch(ex)
    {}
    ajax.drawing.GraphicsJava.superClass.dispose.call(this);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.toString = function()
{
    return "ajax.drawing.GraphicsJava";
}



/*****************************************************************************
// Support Entire Public Applet Interface and marshall calls to applet
/*****************************************************************************/

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.hasLoaded = function()
{
    if (!this._applet.hasLoaded || !this._applet.hasLoaded())
        return false;
            
    return true;    
}



//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.setColor = function(colour)
{///<param name="colour" type="ajax.drawing.Colour"/>
    return this._applet.setColor(colour.r, colour.g, colour.b);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.setStroke = function(val)
{}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawLine = function(x1, y1, x2, y2)
{
    return this._applet.drawLine(x1, y1, x2, y2);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawPolyline3 = function(x1, y1, x2, y2, x3, y3)
{
    return this._applet.drawPolyline3(x1, y1, x2, y2, x3, y3);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawRect = function(x, y, width, height)
{
    return this._applet.drawRect(x, y, width, height);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.fillRect = function(x, y, width, height)
{
    return this._applet.fillRect(x, y, width, height);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawPolygon3 = function(x1, y1, x2, y2, x3, y3)
{
    return this._applet.drawPolygon3(x1, y1, x2, y2, x3, y3);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.fillPolygon3 = function(x1, y1, x2, y2, x3, y3)
{
    return this._applet.fillPolygon3(x1, y1, x2, y2, x3, y3);
}

ajax.drawing.GraphicsJava.prototype.fillArc = function(x, y, width, height, startAngle, arcAngle)
{
    return this._applet.fillArc(x, y, width, height, startAngle, arcAngle);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawEllipse = function(x, y, width, height)
{
    return this._applet.drawEllipse(x, y, width, height);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.fillEllipse = function(x, y, width, height)
{
    return this._applet.fillEllipse(x, y, width, height);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.setFont = function(fontFamily, size, style)
{
    return this._applet.setFont(fontFamily, size, style);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawString = function(str, x, y)
{
    return this._applet.drawString(str, x, y);    
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.addImage = function(src)
{
    return this._applet.addImage(src);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.removeImage = function(src)
{
    return this._applet.removeImage(src);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawImage = function(img, x, y, width, height)
{
    return this._applet.drawImage(img.src, x, y, width, height);
}

//-----------------------------------------------------------------------------

ajax.drawing.GraphicsJava.prototype.drawImageToRect = function(domImage, destinationRect)
{
    var r = ajax.geometry.shapes.Rectangle.cast(destinationRect);
    this.drawImage(domImage, r.x, r.y, r.width, r.height);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.drawImageFromRect = function(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
{
    // We don't support this yet.
}
//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.repaint = function()
{
    return this._applet.repaint();
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.prototype.clear = function(colour)
{///<param name="colour" type="ajax.drawing.Colour"/>
    return this._applet.clear(colour.r, colour.g, colour.b);
}

//-----------------------------------------------------------------------------
ajax.drawing.GraphicsJava.cast = function(obj)
{///<returns type="ajax.drawing.GraphicsJava"/>
    return obj;    
}



